//
//  HJRepairtGroupPartViewCell.m
//  GAMCAfterSales
//
//  Created by 贺佳 on 2018/8/1.
//  Copyright © 2018年 glendale. All rights reserved.
//

#import "HJRepairtGroupPartViewCell.h"

#import "popContentViewController.h"
#import "DDPopoverBackgroundView.h"

#import "HJLocalDataManager.h"

#import "HJCommonModel.h"
@interface HJRepairtGroupPartViewCell()<ContentViewControllerDelegate>{
    
    //下拉菜单属性
    popContentViewController *contentCon;
    UIPopoverController *popCon;
}
/**
 维修组合代码
 */
@property(nonatomic,weak)UILabel*repairtCodeTitleLabel;
/**
 维修项目名称
 */
@property(nonatomic,weak)UILabel*repairtNameLabel;
/**
 维修类型
 */
@property(nonatomic,weak)UILabel*repairtTypeLabel;
/**
 标准工时
 */
@property(nonatomic,weak)UILabel*workTimeLabel;
/**
 工时单价按钮
 */
@property(nonatomic,weak)UIButton *workPriceViewBtn;
/**
 工时单价缓存数据
 */
@property(nonatomic,strong)NSMutableArray*labourPrices;
@end
@implementation HJRepairtGroupPartViewCell
/** 工时单价 */
-(NSMutableArray *)labourPrices{
    if (_labourPrices == nil) {
        NSMutableArray *arr = [HJLocalDataManager sharedInstance].cacheDict[@"TM_LABOUR_PRICE"];
        _labourPrices=[HJCommonModel mj_objectArrayWithKeyValuesArray:arr];
    }
    return _labourPrices;
}
UITablecellM(@"HJRepairtGroupPartViewCell")
-(void)setUI{
    self.selectionStyle=UITableViewCellSelectionStyleNone;
    //维修组合代码
    UILabel*repairtCodeTitleLabel=[[UILabel alloc]init];
    repairtCodeTitleLabel.textAlignment=NSTextAlignmentCenter;
    repairtCodeTitleLabel.font=HJFont(13);
    self.repairtCodeTitleLabel=repairtCodeTitleLabel;
    [self.contentView addSubview:repairtCodeTitleLabel];
    
    //维修项目名称
    UILabel*repairtNameLabel=[[UILabel alloc]init];
    repairtNameLabel.textAlignment=NSTextAlignmentCenter;
    repairtNameLabel.font=HJFont(13);
    self.repairtNameLabel=repairtNameLabel;
    [self.contentView addSubview:repairtNameLabel];
    
    //维修类型
    UILabel*repairtTypeLabel=[[UILabel alloc]init];
    repairtTypeLabel.textAlignment=NSTextAlignmentCenter;
    repairtTypeLabel.font=HJFont(13);
    self.repairtTypeLabel=repairtTypeLabel;
    [self.contentView addSubview:repairtTypeLabel];
    
    //标准工时
    UILabel*workTimeLabel=[[UILabel alloc]init];
    workTimeLabel.textAlignment=NSTextAlignmentCenter;
    workTimeLabel.font=HJFont(13);
    self.workTimeLabel=workTimeLabel;
    [self.contentView addSubview:workTimeLabel];
    
    //工时单价
    UIView*workPriceView=[[UIView alloc]init];
    [self.contentView addSubview:workPriceView];
    
    UIButton *workPriceViewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.workPriceViewBtn=workPriceViewBtn;
    workPriceViewBtn.backgroundColor = [UIColor clearColor];
    [workPriceViewBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    workPriceViewBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [workPriceViewBtn setBackgroundImage:[UIImage imageNamed:@"10.png"] forState:UIControlStateNormal];
    [workPriceViewBtn addTarget:self action:@selector(clickSelectWorkPrice:) forControlEvents:UIControlEventTouchUpInside];
    [workPriceView addSubview:workPriceViewBtn];
    [workPriceViewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(workPriceView);
    }];
    
    WEAKSELF;
    [self.contentView.subviews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    [self.contentView.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(weakSelf.contentView);
    }];
    
}

-(void)clickSelectWorkPrice:(UIButton*)btn{
    if (!contentCon)
    {
        contentCon=[[popContentViewController alloc] initWithNibName:@"popContentViewController" bundle:nil];
        contentCon.delegate = self;
    }
        contentCon.btnSender=btn;

        popCon = [[UIPopoverController alloc] initWithContentViewController:contentCon];
        [DDPopoverBackgroundView setTintColor:[UIColor colorWithRed:234 green:248 blue:222 alpha:0.9]];
        [DDPopoverBackgroundView setArrowBase:30.0];//箭头
        [DDPopoverBackgroundView setArrowHeight:15.0];
        [DDPopoverBackgroundView setContentInset:1.0];

        [popCon setPopoverBackgroundViewClass:[DDPopoverBackgroundView class]];
        [popCon setPopoverContentSize:CGSizeMake(btn.frame.size.width, 280)];

        [popCon presentPopoverFromRect:CGRectMake(40, 15, 70, 20) inView:btn permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        NSMutableArray*source=[NSMutableArray array];
        if (btn == self.workPriceViewBtn){
            for (HJCommonModel*model in self.labourPrices) {
                [source addObject:model.NAME];
            }
        }
        contentCon.list = source;
        [contentCon.myTableView reloadData];
}
-(void)setTM_PACKAGE_LABOURModel:(HJRepairtGroupTM_PACKAGE_LABOURModel *)TM_PACKAGE_LABOURModel
{
    _TM_PACKAGE_LABOURModel=TM_PACKAGE_LABOURModel;
    self.repairtCodeTitleLabel.text=TM_PACKAGE_LABOURModel.MODEL_LABOUR_CODE;
    self.repairtNameLabel.text=TM_PACKAGE_LABOURModel.LABOUR_NAME;
    self.repairtTypeLabel.text=TM_PACKAGE_LABOURModel.REPAIR_TYPE_CODE;
    
    self.workTimeLabel.text=TM_PACKAGE_LABOURModel.STD_LABOUR_HOUR;
    
    [self.workPriceViewBtn setTitle:TM_PACKAGE_LABOURModel.LABOUR_PRICE forState:UIControlStateNormal];
    
}
#pragma mark --ContentViewControllerDelegate
//弹出的回调方法
-(void)selectDelegate:(NSInteger)row btn:(UIButton *)btn
{
    if (btn == self.workPriceViewBtn){
//        [self computeAmout];
         [btn setTitle:[self.labourPrices[row] NAME] forState:UIControlStateNormal];
        self.TM_PACKAGE_LABOURModel.LABOUR_PRICE=[self.labourPrices[row] NAME];
        
        self.TM_PACKAGE_LABOURModel.LABOUR_AMOUNT=[NSString stringWithFormat:@"%.2f",[self.TM_PACKAGE_LABOURModel.LABOUR_PRICE floatValue]*[self.TM_PACKAGE_LABOURModel.STD_LABOUR_HOUR floatValue]];
    }
    
    //清空数据，退出控件
    [popCon dismissPopoverAnimated:YES];
    popCon = nil;
}
@end
